﻿using AutoMapper;
using ManagementApi.Models;
using ManagementApi.Models.System.Role;
using ManagementApi.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Modules.System.Menu;
using Modules.System.Role;
using PmSoft.Utilities;

namespace ManagementApi.Controllers.SystemManage
{
    /// <summary>
    /// 角色相关业务接口
    /// </summary>
    [Route("/api/v1/roles")]
    [ApiController]
    [Authorize(Roles = "SysUser")]
    public class RoleController : ControllerBase
    {
        private readonly RoleService roleService;
        private readonly IMapper mapper;
        private readonly MenuService menuService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleService"></param>
        /// <param name="mapper"></param>
        public RoleController(
            RoleService roleService,
             IMapper mapper,
             MenuService menuService
            )
        {
            this.roleService = roleService;
            this.mapper = mapper;
            this.menuService = menuService;
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns></returns>
        [HttpGet("page")]
        public ApiResult<PageDto<RoleDto>> GetRoles([FromQuery] RoleQuery query, int pageNum = 1, int pageSize = 20)
        {
            var roles = roleService.GetRoles(query, pageNum, pageSize);
            var dtos = roles.AsPageDto(source => { return source.AsRoleDto(); });
            return ApiResult<PageDto<RoleDto>>.Success(dtos);
        }

        /// <summary>
        /// 角色表单数据
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        [HttpGet("{id}/form")]
        public ApiResult<RoleEditForm> GetRole(int id)
        {
            var menu = roleService.GetSysRole(id);
            if (menu == null)
                return ApiResult<RoleEditForm>.Failure("角色数据不存在");
            RoleEditForm roleEditForm = new RoleEditForm();
            mapper.Map(menu, roleEditForm);
            return ApiResult<RoleEditForm>.Success(roleEditForm);
        }

        /// <summary>
        /// 角色下拉
        /// </summary>
        /// <returns></returns>
        [HttpGet("options")]
        public ApiResult<IEnumerable<VueTree<int>>> GetRoleOptions()
        {
            var trees = roleService.GetAllRoles().AsTrees();
            return ApiResult<IEnumerable<VueTree<int>>>.Success(trees);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost("create")]
        [PermissionCheck("sys:role:add")]
        public ApiResult Add(RoleEditForm form)
        {
            SysRole role = new SysRole();
            mapper.Map(form, role);
            roleService.CreateRole(role);
            return ApiResult.Success("创建成功");
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        [PermissionCheck("sys:role:edit")]
        public ApiResult Update(int id, RoleEditForm form)
        {
            var role = roleService.GetSysRole(id);
            if (role == null) return ApiResult.Failure("角色不存在");
            mapper.Map(form, role);
            role.RoleId = id;
            roleService.UpdateRole(role);
            return ApiResult.Success("编辑成功");
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{ids}")]
        [PermissionCheck("sys:role:delete")]
        public ApiResult Delete(string ids)
        {
            roleService.DeleteRoles(ValueUtility.ConvertToList<int>(ids));
            return ApiResult.Success("删除成功");
        }

        /// <summary>
        /// 获取角色的菜单ID集合
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}/menuIds")]
        public ApiResult<int[]> GetMenuIds(int id)
        {
            var result = roleService.GetRoleMenuIds(id).ToArray();
            return ApiResult<int[]>.Success(result);
        }

        /// <summary>
        /// 分配菜单权限给角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="menuIds"></param>
        /// <returns></returns>
        [HttpPut("{id}/menus")]
        [PermissionCheck("sys:role:auth")]
        public ApiResult UpdateRoleMenus(int id, int[] menuIds)
        {
            var role = roleService.GetSysRole(id);
            if (role == null) return ApiResult.Success("当前角色不存在");
            roleService.UpdateRoleMenus(id, role.Code, menuIds);
            return ApiResult.Success("授权成功");
        }
    }
}